<template>
    <!--  -->
    <el-button plain @click="dialogFormVisible = true">
        新增
    </el-button>
    <el-dialog v-model="dialogFormVisible" title="编辑" width="500">
        <el-form :model="form">
            <!-- 名字 -->
            <el-form-item label="名字" :label-width="formLabelWidth">
                <el-input v-model="form.name" autocomplete="off" />
            </el-form-item>
            <!-- 分类 -->
            <el-form-item label="分类" :label-width="formLabelWidth">
                <el-select v-model="form.region" placeholder="Please select a zone">
                    <el-option label="Zone No.1" value="shanghai" />
                </el-select>
            </el-form-item>
            <!-- 简介 -->
            <el-form-item label="简介" :label-width="formLabelWidth">
                <el-input v-model="form.jianjie" autocomplete="off" />
            </el-form-item>
            <!-- 图片 -->
            <el-upload class="avatar-uploader" action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
                :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                <img v-if="imageUrl" :src="imageUrl" class="avatar" />
                <el-icon v-else class="avatar-uploader-icon">
                    <Plus />
                </el-icon>
            </el-upload>

            <!--  -->
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="dialogFormVisible = false">Cancel</el-button>
                <el-button type="primary" @click="dialogFormVisible = false">
                    Confirm
                </el-button>
            </div>
        </template>
    </el-dialog>
    <!--  -->
    <div>
        <el-table :data="tableData" style="width: 100%">
            <!-- id 列 -->
            <el-table-column prop="id" label="序号" width="100" />
            <!-- 图片列 -->
            <el-table-column prop="coverImage" label="图片" width="150">
                <template #default="{ row }">
                    <img :src="row.coverImage" alt="商品图片" style="width: 100px; height: auto;" />
                </template>
            </el-table-column>
            <!-- 商品名称列 -->
            <el-table-column prop="name" label="商品" width="150" />
            <!-- 价格 -->
            <el-table-column prop="price" label="价格" width="100" />
            <!-- 库存-->
            <el-table-column prop="amount" label="库存" width="100" />

            <el-table-column fixed="right" label="编辑" min-width="120">
                <template #default="{ row }">
                    <el-button link type="primary" size="small" @click="handleDelete(row)">
                        删除
                    </el-button>
                    <el-button link type="primary" size="small" @click="handleEdit(row)">
                        修改
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页组件 -->
        <el-pagination background layout="prev, pager, next, jumper" :current-page="currentPage" :page-size="pageSize"
            :total="total" @current-change="handleCurrentChange" style="margin-top: 20px; justify-content: center" />
    </div>
</template>

<script lang="ts" setup>

import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'

import type { UploadProps } from 'element-plus'

const imageUrl = ref('')


import { ref, onMounted, reactive } from 'vue'
import axios from 'axios'

const tableData = ref([])
const currentPage = ref(1)
const pageSize = ref(5) // 每页显示10条
const total = ref(0) // 总数据量

// 
const dialogTableVisible = ref(false)
const dialogFormVisible = ref(false)
const formLabelWidth = '140px'

const form = reactive({
    name: '',
    region: '',
    date1: '',
    date2: '',
    delivery: false,
    type: [],
    resource: '',
    desc: '',
})

// 
const handleAvatarSuccess: UploadProps['onSuccess'] = (
    response,
    uploadFile
) => {
    imageUrl.value = URL.createObjectURL(uploadFile.raw!)
}

const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
    if (rawFile.type !== 'image/jpeg') {
        ElMessage.error('Avatar picture must be JPG format!')
        return false
    } else if (rawFile.size / 1024 / 1024 > 2) {
        ElMessage.error('Avatar picture size can not exceed 2MB!')
        return false
    }
    return true
}
// 
// 加载数据
const loadData = async () => {
    try {
        const token = localStorage.getItem('token')
        const response = await axios.get('http://localhost:1337/api/v1/admin/product', {
            params: {
                page: currentPage.value,
                per: pageSize.value
            },
            headers: { token }
        })

        // 如果返回空数据且不是第一页
        if (response.data.data.length === 0 && currentPage.value > 1) {
            currentPage.value--
            ElMessage.warning('已经是最后一页了')
            return
        }

        tableData.value = response.data.data
        total.value = response.data.total // 假设接口返回总条数
        console.log(response.data);

    } catch (error) {
        ElMessage.error('数据加载失败')
        console.error(error)
    }
}

// 分页变化处理
const handleCurrentChange = (val) => {
    currentPage.value = val
    loadData()
}

// 删除操作
const handleDelete = (row) => {
    // 添加你的删除逻辑
}

// 修改操作
const handleEdit = (row) => {
    // 添加你的修改逻辑
}


// 新建商品

// 初始化加载{数据
onMounted(() => {
    loadData()
})
</script>

<style>
.el-pagination {
    margin-top: 20px;
}

img {
    border-radius: 4px;
}

.avatar-uploader .avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
}
</style>